/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package runze.shiro.domain;

import org.apache.shiro.crypto.hash.Sha256Hash;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * Simple class that represents any User domain entity in any application.
 *
 * <p>Because this class performs its own Realm and Permission checks, and these can happen frequently enough in a
 * production application, it is highly recommended that the internal User {@link #getRoles} collection be cached
 * in a 2nd-level cache when using JPA and/or Hibernate.  The hibernate xml configuration for this sample application
 * does in fact do this for your reference (see User.hbm.xml - the 'roles' declaration).</p>
 */
@Entity
@Table(name="t_user", indexes=
	{@Index(name="idx_users_username", columnList="username"), 
			@Index(name="idx_users_email", columnList="email")})
@Cache(usage= CacheConcurrencyStrategy.READ_WRITE)
public class User  {

	@Id @GeneratedValue
    private Integer id;
	
	@Basic(optional=false)
	@Column(length=100)
    private String username;
	
	@Basic(optional=false)
    private String email;
	
	@Basic(optional=false)
	@Column(length=255)
    private String password;
	
	@ManyToMany
	@JoinTable(name="t_user_role", joinColumns=@JoinColumn(name="uid", nullable=false)
	, inverseJoinColumns=@JoinColumn(name="rid", nullable=false))
	@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
    private Set<Role> roles = new HashSet<Role>();
	
	public User() { }
	public User(Integer id, String username, String password, String email) {
		this.id = id;
		this.username = username;
		this.email = email;
		this.password = password;
	}
	public User(String username, String email, String password) {
		this.username = username;
		this.email = email;
		this.password = password;
	}
	public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    /**
     * Returns the username associated with this user account;
     *
     * @return the username associated with this user account;
     */
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * Returns the password for this user.
     *
     * @return this user's password
     */
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
    	if (StringUtils.hasText(password)) {
    		String hex = new Sha256Hash(password).toHex();
    		this.password = hex;
    	} else {
    		this.password = null;	// 抛异常
    	}
    }

    public Set<Role> getRoles() {
        return roles;
    }
    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

}


